import ddddocr
import requests
import execjs
import json

ocr = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
with open('./parse/3.云片网验证码.js', 'r', encoding='utf-8') as f:
    js_str = f.read()
js_code = execjs.compile(js_str, cwd='./parse')

img_data = js_code.call('get_captcha_url')
img_res = requests.get(img_data)
img_res_json = json.loads(img_res.text[8:-1])
print(img_res_json)
bg_url = img_res_json['data']['bg']
front_url = img_res_json['data']['front']
token = img_res_json['data']['token']
bg_res = requests.get(bg_url)
with open('./static/yzm/yunpian_bg.png', 'wb') as f:
    f.write(bg_res.content)

front_res = requests.get(front_url)
with open('./static/yzm/yunpian_front.png', 'wb') as f:
    f.write(front_res.content)

offset = ocr.slide_match(front_res.content, bg_res.content)['target'][0] + 12
print(f'偏移量:{offset}',token)

verify_url = js_code.call('get_verify_url',offset,token)
verify_res = requests.get(verify_url)
print(verify_res.text)